package com.nursing.service;

import com.nursing.Dao.AdminDao;
import com.nursing.bean.Admin;
import com.nursing.model.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdminService {

    private final AdminDao dao;

    @Autowired
    public AdminService(AdminDao dao) {
        this.dao = dao;
    }

    public Result<List<Admin>> getAll(){
        return Result.success(dao.getAll());
    }

    public Result<Admin> login(String userName , String password){

        Admin user = dao.verify(userName,password);
        if(user == null){
            return Result.fail(400,"用户名或密码错误！");
        }
        user.setPassword("******");
        return Result.success(user);
    }

    public Result<Admin> changePassword(Admin admin){
        Admin user = dao.getById(admin.getId());
        if(user == null){
            return Result.fail(400,"用户名错误！");
        }
        admin.setPassword(admin.getPassword());
        dao.changePassword(admin);
        admin.setPassword("******");
        return Result.success(admin);
    }

    public Admin register (Admin admin){
        admin.setPassword(DigestUtils.md5Hex(admin.getPassword()));
        dao.insert(admin);
        admin.setPassword("******");
        return admin;

    }

}
